package net.wanho.zaixian.course.service.impl;

import net.wanho.zaixian.course.dao.CategoryDao;
import net.wanho.zaixian.course.service.CategoryService;
import net.wanho.zaixian.pojo.course.Category;
import net.wanho.zaixian.pojo.course.ext.CategoryNode;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;

    @Override
    public List<CategoryNode> getAllCategory() {
        //获取根节点
        Category category = categoryDao.findById("1").get();

        CategoryNode categoryNode = new CategoryNode();

        categoryNode.setId(category.getId());
        categoryNode.setName(category.getName());
        categoryNode.setIsleaf(category.getIsleaf());
        categoryNode.setIsshow(category.getIsshow());
        categoryNode.setLabel(category.getLabel());
        //该根节点的Id就是他的下级category的parentId
        String parentId = category.getId();
        //将parentId作为条件封装到查询条件中
        List<CategoryNode> secondCategoryNodeList = genCategoryNodeByParentId(parentId);

        //遍历二级节点，将二级节点数据强转成CategoryNode,并放到新建的ret集合中
        for(CategoryNode categoryNode1:secondCategoryNodeList){

            String secondParentId = categoryNode1.getId();
            //将parentId作为条件封装到查询条件中
            List<CategoryNode> thirdCategoryNodeList = genCategoryNodeByParentId(secondParentId);
            categoryNode1.setChildren(thirdCategoryNodeList);


        }



        return secondCategoryNodeList;
    }

    private List<CategoryNode> genCategoryNodeByParentId(String parentId){
        Category categoryParam = new Category();
        categoryParam.setParentid(parentId);
        Example<Category> of = Example.of(categoryParam);
        //此次获取的跟节点的子节点
        List<Category> secondCategoryList = categoryDao.findAll(of);

        List<CategoryNode> categoryNodeList = new ArrayList<>();
        for(Category category:secondCategoryList){
            CategoryNode categoryNode = new CategoryNode();
            categoryNode.setId(category.getId());
            categoryNode.setName(category.getName());
            categoryNode.setParentid(category.getParentid());
            categoryNode.setOrderby(category.getOrderby());
            categoryNode.setIsleaf(category.getIsleaf());
            categoryNode.setIsshow(category.getIsshow());
            categoryNode.setLabel(category.getLabel());
            categoryNodeList.add(categoryNode);
        }

        return categoryNodeList;
    }

//    public List<Category> getCategoryTree(List<Category> list){
//        List<Category> rootList = new ArrayList<>();
//        //从所有category中找出根
//        for(Category category:list){
//            if("1".equals(category.getId())){
//                addChildCategory(list,category.getId());
//            }
//        }
//        return null;
//
//    }
//
//
//    public List addChildCategory(List<Category> list,String id){
//
//        List<Category> childList = new ArrayList<>();
//        for(Category category:list){
//            if(category.getParentid().equals(id)){
//                List list1 = addChildCategory(list, category.getId());
//                childList.add(list1)
//            }
//        }
//    }

}
